//询价管理

/**
 * 询价单管理视图模型
 */
interface InquiryManagentViewModel extends IBaseViewModel<InquiryManagentQueryTerms> {
    /**
     * 询价状态下拉
     */
    stateDropDown: ValueListItemResponse[];
    /**
     * 调度专员下拉
     */
    dispatchOfficer: EmployeeListItemResponse[];
    /**
     * ui-grid控制
     */
    gridOptions: uiGrid.IGridOptionsOf<InquiryListItemResponse>;
    /**
     * 操作
     */
    inquiryManageEdit: (id: string) => void;
    /**
     * 跳转
     */
    skipExport: () => void;
    /**
     * 查询
     */
    query: () => void;
    /**
     * 列表高
     */
    heightA:number;

}

/**
 * 查询条件
 */
interface InquiryManagentQueryTerms {
    /**
     * 总询价单编号
     */
    inquiryId: string;
    /**
     * 询价子单编号
     */
    inquiryChildId: string;
    /**
     * 客户单位名称
     */
    clientName: string;
    /**
     *  调度专员编号
     */
    dispatcherid: string;
    /**
     *  商务专员编号
     */
    businessaffairsid: string;
    /**
     * 下单起始时间时间
     */
    beginTime: string;
    /**
     * 下单起始时间时间
     */
    endTime: string;
    /**
     * 发货地址
     */
    startAddress: string;
    /**
     * 送货地址
     */
    endAddress: string;
    /**
     * 询价状态
     */
    state: string;
}

class InquiryManagentController extends ListController<InquiryManagentViewModel, InquiryManagentQueryTerms>{
    gridApi: uiGrid.IGridApiOf<InquiryListItemResponse>;
    orderBy: number = 7;
    asc: boolean = false;
    skip:number=0;

    constructor($scope: InquiryManagentViewModel, private inquiryService: InquiryService, private valueService: IValueService, private employeeService: IEmployeeService, private $ngBootbox: BootboxService, 
    private $state: angular.ui.IStateService,private routeService:routeService,private $location:ng.ILocationService,private exportService:IexportService) {
        super($scope);
        this.init();
    }

    init(): void {
        super.init();

        //this.$scope.skipExport = this.exports;
        this.$scope.inquiryManageEdit = this.inquiryManageEdit;

        this.$scope.queryTerms = {
            inquiryId: "",
            inquiryChildId: "",
            clientName: "",
            dispatcherid: "",
            businessaffairsid: "",
            beginTime: "",
            endTime: "",
            startAddress: "",
            endAddress: "",
            state: ""
        };

        this.$scope.gridOptions = {
            paginationPageSizes: [10, 20, 30],
            useExternalPagination: true,
            useExternalSorting: true,
            enableColumnResizing: true,
            columnDefs: [
                { displayName: "总询价编号", field: 'inquiryId', enableColumnMenu: false, cellTooltip: true, headerTooltip: true, width: '9%' },
                { displayName: "子询价编号", field: 'inquiryChildId', enableColumnMenu: false, cellTooltip: true, headerTooltip: true, width: '10%' },
                { displayName: "客户单位", field: 'clientName', enableColumnMenu: false, cellTooltip: true, headerTooltip: true, width: '11%' },
                { displayName: '发货地址', field: 'startAddress', enableColumnMenu: false, cellTooltip: true, headerTooltip: true, enableSorting: false, width: '10%' },
                { displayName: '送货地址', field: 'endAddress', enableColumnMenu: false, cellTooltip: true, headerTooltip: true, enableSorting: false, width: '10%' },
                { displayName: '货物名称', field: 'goodsName', enableColumnMenu: false, cellTooltip: true, headerTooltip: true },
                { displayName: '货物数量', field: 'goodsQuantity', enableColumnMenu: false, cellTooltip: true, headerTooltip: true },
                { displayName: '所需车长', field: 'carLength', enableColumnMenu: false, cellTooltip: true, headerTooltip: true, },
                { displayName: '应付报价', field: 'payableQuotation', enableColumnMenu: false, cellTooltip: true, headerTooltip: true, },
                { displayName: '应付报价时间', field: 'payableQuotationTime', enableColumnMenu: false, cellTooltip: true, headerTooltip: true, },
                { displayName: '调度专员', field: 'dispatchOfficer', enableColumnMenu: false, cellTooltip: true, headerTooltip: true },
                { displayName: '下单时间', field: 'createTime', enableColumnMenu: false, cellTooltip: true, headerTooltip: true },
                { displayName: '紧急程度', field: 'urgency', enableColumnMenu: false, cellTooltip: true, headerTooltip: true },
                { displayName: '询价状态', field: 'state', enableColumnMenu: false, cellTooltip: true, headerTooltip: true },
                {
                    displayName: "操作",
                    field: '操作',
                    cellTemplate: '<a  ng-click="grid.appScope.inquiryManageEdit(row.entity.id)" class="fa fa-eye m-l-xs  primary text-info" title="查看详情 "></a>', enableColumnMenu: false, enableSorting: false
                }],
            onRegisterApi: (gridApi) => {
                this.gridApi = gridApi;
                gridApi.pagination.on.paginationChanged(this.$scope,(skipSize,pageSize)=>{
                    this.paginationChanged(skipSize,pageSize,this.$state);
                });
                // gridApi.pagination.on.paginationChanged(this.$scope, this.paginationChanged);
                gridApi.core.notifyDataChange("options");
                gridApi.core.on.sortChanged(this.$scope, this.sortChange);
            }
        };

        this.$scope.query = this.query;
        this.query();
        this.loadStateDropDown();
        this.loadDepartmentDropDown();
        $('#opendataTime').click(function() {
            $('#dataTime').datetimepicker('show');
        });

        $('#opendataTimeEnd').click(function() {
            $('#dataTimeEnd').datetimepicker('show');
        });
    }

    localHistory():void{
        super.localHistory(this.$state);
    }
    getHistory():void{
        super.getHistory(this.$state,this.$location)
    }


    loadData(queryTerms: InquiryManagentQueryTerms, skip: number, count: number): void {
        this.inquiryService.getList(this.orderBy.toString(), this.asc, queryTerms.inquiryId,
            queryTerms.inquiryChildId, queryTerms.clientName, queryTerms.dispatcherid, queryTerms.businessaffairsid,
            queryTerms.startAddress, queryTerms.endAddress, queryTerms.state, queryTerms.beginTime, queryTerms.endTime, skip, count).then(result => {
                this.$scope.gridOptions.totalItems = result.data.total;
                this.$scope.gridOptions.data = result.data.data;

                this.$scope.heightA = (this.$scope.gridOptions.data.length * 39)+95;

                if (skip == 0) {
                    this.$scope.gridOptions.paginationCurrentPage = 1;
                }
                this.skip = skip;

                this.$scope.querying = false;
            }, error => {
                this.$scope.querying = false;
            });
    }

    /**
     * 排序事件触发
     */
    sortChange = (grid: uiGrid.IGridInstanceOf<InquiryListItemResponse>, sortColumns: Array<uiGrid.IGridColumnOf<InquiryListItemResponse>>) => {
        if (sortColumns.length == 0) {
            this.reload();
        } else {
            let sortItem = sortColumns[0];
            switch (sortItem.name) {
                case "inquiryId": {
                    this.orderBy = 1;
                } break;
                case "inquiryChildId": {
                    this.orderBy = 2;
                } break;
                case "clientName": {
                    this.orderBy = 3;
                } break;
                case "goodsName": {
                    this.orderBy = 4;
                } break;
                case "goodsQuantity": {
                    this.orderBy = 5;
                } break;
                case "carLength": {
                    this.orderBy = 6;
                } break;
                case "createTime": {
                    this.orderBy = 7
                } break;
                case "urgency": {
                    this.orderBy = 8;
                } break;
                case "state": {
                    this.orderBy = 9;
                } break;
            }
            this.asc = sortItem.sort.direction == "asc";
            this.reload();
        }
    }

    loadStateDropDown(): void {
        this.$scope.stateDropDown = this.valueService.getInquiryStatusList().data;       
    }

    /**
     * 填充调度专员下拉
     */
    loadDepartmentDropDown(): void {
        this.employeeService.getbytype("2",0, -1).then(result => {
            this.$scope.dispatchOfficer = result.data.data;
            //console.log(result.data.data);
        });
    }

    /**
     * 编辑询价单管理跳转
     */
    inquiryManageEdit(id: string): void {
        this.$state.go("app.inquiry.Inquiry_pay", { id: id, name: "manage" ,skip:this.skip});//跳转的状态码需要修改
    }


    /**
    * 询价单管理导出跳转
    */
    exportFunc=()=>{
        if(this.$scope.gridOptions.totalItems>60000){
            this.$ngBootbox.alert('导出数据不可超过6万条');
            return;
        }else{
            this.exportService.exportInquiryList(this.orderBy.toString(),this.asc,this.queryTerms.inquiryId,this.queryTerms.inquiryChildId,
            this.queryTerms.clientName,this.queryTerms.dispatcherid,this.queryTerms.startAddress,this.queryTerms.endAddress,
            this.queryTerms.state,this.queryTerms.beginTime,this.queryTerms.endTime)
        }
        
    }

}

angular.module("tms").controller("inquiryManagentController", ["$scope","inquiryService","valueService", "employeeService",  "$ngBootbox","$state","routeService","$location", "exportService", InquiryManagentController]);
